<!--
 * @Author: captern
 * @@Gitee: https://gitee.com/captern
 * @@websit: http://www.captern.cn/
 * @Date: 2021-02-05 15:14:26
 * @LastModifiedBy: captern
 * @LastEditTime: 2021-04-12 19:11:55
 * @Description:
-->
<script>

import helper from './utils/helper.js';
import {
	getUserToken,
	getUserInfo
} from './tool/userTool.js'
export default {
	globalData: {
		phoneNum:null,
		wxUserInfo:null,
		userInfo:null,
		systemInfo: {
			model: '',
			platform: '',
			system: '',
			language: '',
			version: '',
			latitude: '',
			longitude: '',
			titleBarHeight: 44,
			statusBarHeight: 0,
			windowWidth: 0,
			windowHeight: 0,
			navBarHeight: 0
		}
	},
	 onLaunch(options) {
		uni.showToast({
			title: "加载中"
		})
		var that = this;
		// 小程序分享卡片 链接中拼接参数
		let phoneNum = uni.getStorageSync('phoneNum')
		if (phoneNum) {
		   this.globalData.phoneNum = phoneNum;
		}
		let wxUserInfo = uni.getStorageSync('wxUserInfo')
		if (wxUserInfo) {
		   this.globalData.wxUserInfo = wxUserInfo;
		}

		// 获取设备信息
		uni.getSystemInfo({
			success: res => {
				this.globalData.systemInfo.model = res.model;
				this.globalData.systemInfo.platform = res.platform;
				this.globalData.systemInfo.system = res.system;
				this.globalData.systemInfo.language = res.language;
				this.globalData.systemInfo.version = res.version;
				this.globalData.systemInfo.titleBarHeight = 44;
				this.globalData.systemInfo.statusBarHeight = res.statusBarHeight;
				this.globalData.systemInfo.windowWidth = res.windowWidth;
				this.globalData.systemInfo.windowHeight = res.windowHeight;
				this.globalData.systemInfo.navBarHeight = res.statusBarHeight + 44;

			}
		});
		//更新
		that.updataMp();


		uni.login({
		    success: async res => {
				let tokenRes = await getUserToken(res.code);
				let aa = await getUserInfo()
				if(aa.nickname && aa.nickname != ''){
					let wxUserInfo =  uni.getStorageSync('wxUserInfo');
					if(!wxUserInfo){
						wxUserInfo = {
							nickName:  aa.nickname,
							avatarUrl: aa.headimgurl
						}
					}else {
						wxUserInfo.nickName = aa?.nickname
						wxUserInfo.avatarUrl = aa?.headimgurl
					}
					getApp().globalData.wxUserInfo = wxUserInfo;
					uni.setStorageSync('wxUserInfo', wxUserInfo);
				}
				if(aa.phone && aa.phone != ''){
					getApp().globalData.phoneNum = aa.phone;
					uni.setStorageSync('phoneNum', aa.phone);
				}
				if(tokenRes){
					uni.hideToast()
					this.$isResolve()
				}
			}
		})
	},
	methods: {
		updataMp: function() {
			// 更新
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function(res) {
				console.log(res);
			});
			updateManager.onUpdateReady(function() {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					}
				});
			});
		}
	}
};
</script>

<style>
@import './App.css';
</style>
